package org.example.poi.POIEXCEL;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

//03版本xls和07版本xlsx有兼容性问题
//hssfworkbood 是03版本07版本都可以读取
//xlsxworkbook 是07版本
public class two {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("d:\\1.xls");
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 0; i < rows; i++) {
            HSSFRow row = sheet.getRow(i);
            int cells = row.getPhysicalNumberOfCells();
            for (int j = 0; j < cells; j++) {
                HSSFCell cell = row.getCell(j);
                int type = cell.getCellType();
                Object value = "";
                switch (type) {
                    case HSSFCell.CELL_TYPE_STRING:
                        value = cell.getStringCellValue();
                        break;
                    case HSSFCell.CELL_TYPE_BOOLEAN:
                        value = cell.getBooleanCellValue();
                        break;
                    //无int类型，只能使用string类型装数字||默认数字类型double
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        value = cell.getStringCellValue();
                        break;
                }
                System.out.print(value + "");
            }
            System.out.println();
        }
        fileInputStream.close();
    }
}
